package dev.ece.suit.hexmap.terrain.type;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import dev.ece.core.light.Material;

@Deprecated
public class DefaultTerrainTypeFactory implements TerrainTypeFactory {
	
	protected static final Logger LOG = LogManager.getLogger(DefaultTerrainTypeFactory.class);
	
	protected static final Map<String, Class<? extends TerrainType>> TERRAIN_TYPE_MAP = new ConcurrentHashMap<String, Class<? extends TerrainType>>();
	
	static {
		TERRAIN_TYPE_MAP.put("default", DefaultTerrainType.class);
		TERRAIN_TYPE_MAP.put("hill", DefaultHillTerrainType.class);
		TERRAIN_TYPE_MAP.put("highhill", DefaultHighHillTerrainType.class);
		TERRAIN_TYPE_MAP.put("beach", DefaultBeachTerrainType.class);
		TERRAIN_TYPE_MAP.put("water", DefaultWaterTerrainType.class);
		TERRAIN_TYPE_MAP.put("reef", DefaultWaterReefTerrainType.class);
	}
	
	@Override
	public TerrainType createTerrainType(String type, int index) {
		Class<? extends TerrainType> clazz = TERRAIN_TYPE_MAP.get(type);
		if(clazz != null) {
			Constructor<? extends TerrainType> constructor = null;
			try {
				constructor = clazz.getConstructor(int.class);
				return constructor.newInstance(index);
			} catch (NoSuchMethodException e) {
				LOG.error("地形类型创建失败", e);
			} catch (SecurityException e) {
				LOG.error("地形类型创建失败", e);
			} catch (InstantiationException e) {
				LOG.error("地形类型创建失败", e);
			} catch (IllegalAccessException e) {
				LOG.error("地形类型创建失败", e);
			} catch (IllegalArgumentException e) {
				LOG.error("地形类型创建失败", e);
			} catch (InvocationTargetException e) {
				LOG.error("地形类型创建失败", e);
			}
		}
		return null;
	}

	@Override
	public TerrainType createTerrainType(String type, Material material, int index) {
		Class<? extends TerrainType> clazz = TERRAIN_TYPE_MAP.get(type);
		if(clazz != null) {
			Constructor<? extends TerrainType> constructor = null;
			try {
				if(material != null) {
					constructor = clazz.getConstructor(Material.class, int.class);
					return constructor.newInstance(material, index);
				}
				constructor = clazz.getConstructor(int.class);
				return constructor.newInstance(index);
			} catch (NoSuchMethodException e) {
				LOG.error("地形类型创建失败", e);
			} catch (SecurityException e) {
				LOG.error("地形类型创建失败", e);
			} catch (InstantiationException e) {
				LOG.error("地形类型创建失败", e);
			} catch (IllegalAccessException e) {
				LOG.error("地形类型创建失败", e);
			} catch (IllegalArgumentException e) {
				LOG.error("地形类型创建失败", e);
			} catch (InvocationTargetException e) {
				LOG.error("地形类型创建失败", e);
			}
		}
		return null;
	}

}
